package net.zhongfu.flowable.helper.database;

import liquibase.CatalogAndSchema;
import liquibase.database.AbstractJdbcDatabase;
import liquibase.database.DatabaseConnection;
import liquibase.exception.DatabaseException;
import liquibase.statement.DatabaseFunction;
import liquibase.statement.SqlStatement;
import liquibase.statement.core.RawCallStatement;
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.Table;

public class DmDatabase extends AbstractJdbcDatabase {
    public static final String PRODUCT_NAME = "DM DBMS";

    public DmDatabase() {
        super.unquotedObjectsAreUppercased = true;
        //noinspection HardCodedStringLiteral
        super.setCurrentDateTimeFunction("SYSTIMESTAMP");
        // Setting list of Oracle's native functions
        //noinspection HardCodedStringLiteral
        dateFunctions.add(new DatabaseFunction("SYSDATE"));
        //noinspection HardCodedStringLiteral
        dateFunctions.add(new DatabaseFunction("SYSTIMESTAMP"));
        //noinspection HardCodedStringLiteral
        dateFunctions.add(new DatabaseFunction("CURRENT_TIMESTAMP"));
        //noinspection HardCodedStringLiteral
        super.sequenceNextValueFunction = "%s.nextval";
        //noinspection HardCodedStringLiteral
        super.sequenceCurrentValueFunction = "%s.currval";
    }


    @Override
    public boolean supportsInitiallyDeferrableColumns() {
        return false;
    }

    @Override
    public boolean supportsTablespaces() {
        return false;
    }

    @Override
    public int getPriority() {
        return 1;
    }

    @Override
    public String getShortName() {
        return "dmbase";
    }

    @Override
    protected String getDefaultDatabaseProductName() {
        return PRODUCT_NAME;
    }

    @Override
    public Integer getDefaultPort() {
        return 5236;
    }

    @Override
    public boolean isCorrectDatabaseImplementation(DatabaseConnection conn) throws DatabaseException {
        return PRODUCT_NAME.equalsIgnoreCase(conn.getDatabaseProductName());
    }

    @Override
    public String getDefaultDriver(String url) {
        return "dm.jdbc.driver.DmDriver";
    }

    @Override
    public boolean supportsSequences() {
        return super.supportsSequences();
    }

    @Override
    public boolean getAutoCommitMode() {
        return true;
    }

    @Override
    public String escapeIndexName(String catalogName, String schemaName, String indexName) {
        return indexName;
    }

    @Override
    public String escapeTableName(String catalogName, String schemaName, String tableName) {
        return super.escapeTableName(null, schemaName, tableName);
    }


    @Override
    public String getDefaultCatalogName() {
        return null;
    }

    @Override
    public boolean supportsCatalogs() {
        return false;
    }

    @Override
    protected boolean canCreateChangeLogTable() throws DatabaseException {
        return false;
    }

    @Override
    public boolean isSystemObject(DatabaseObject example) {
        if (example instanceof Table) {

        }
        return super.isSystemObject(example);
    }

    @Override
    public String getDatabaseChangeLogTableName() {
        return super.getDatabaseChangeLogTableName().toUpperCase();
    }

    @Override
    public String getDatabaseChangeLogLockTableName() {
        return super.getDatabaseChangeLogLockTableName().toUpperCase();
    }

    @Override
    public String getJdbcSchemaName(CatalogAndSchema schema) {
        return String.valueOf(super.getJdbcSchemaName(schema)).toUpperCase();
    }

    @Override
    public String getDefaultSchemaName() {
        return String.valueOf(super.getDefaultSchemaName()).toUpperCase();
    }

    @Override
    protected String getConnectionSchemaName() {
        return String.valueOf(super.getConnectionSchemaName()).toUpperCase();
    }

    @Override
    protected SqlStatement getConnectionSchemaNameCallStatement() {
        return new RawCallStatement("SELECT CURRENT_USER");
    }
}
